// pages/member/vip/myVip.js
const app = getApp()
import { list as wxPageList } from '../../../api/wxPage'
import {
  list
} from '../../../api/vip'
import { find } from '../../../api/article';
import {
  list as tagList,
  tag
} from '../../../api/article';
import {
  getInfo,
} from '../../../api/member'
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // indicatorDots: true, //是否显示面板指示点
    autoplay: false, //是否自动播放
    interval: 6000, //停留时间间隔
    duration: 1000, //播放时长
    previousMargin: '254rpx', //前边距，可用于露出前一项的一小部分，接受 px 和 rpx 值
    nextMargin: '254rpx', //后边距，可用于露出后一项的一小部分，接受 px 和 rpx 值
    circular: true, //是否采用衔接滑动
    currentSwiperIndex: 1, //swiper当前索引
    vipForm: [],
    aboutList: [],
    tagIds: '',
    version: '',
    img: '',
    content: '',
    htmlcontent: '',
    contentType: true,
    vip: "",
    bottonTitle: '',
    tagId: null,
    vipMethod:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadlist()
    this.getInfo()
  },
  loadlist() {
    var _this = this
    let params = {
      'mchId': app.globalData.mchId
    }
    list(params).then(
      res => {
        console.log(res)
        _this.data.vipForm = res.data.data
        _this.initAbout(_this.data.vipForm[_this.data.currentSwiperIndex].name)
        _this.data.vipMethod = _this.data.vipForm[_this.data.currentSwiperIndex].method
        if (_this.data.vipForm[_this.data.currentSwiperIndex].hasOwnProperty('title')) {
          _this.data.tagId = _this.data.vipForm[_this.data.currentSwiperIndex].tagid
          _this.data.bottonTitle = _this.data.vipForm[_this.data.currentSwiperIndex].title
        } else {
          _this.data.tagId = null
          _this.data.bottonTitle = '立即开通'
        }
        _this.setData({
          vipForm: _this.data.vipForm,
          bottonTitle: _this.data.bottonTitle,
          tagId: _this.data.tagId,
          vipMethod:_this.data.vipMethod
        })
      }
    ).catch(error => {
      console.log(error)
    })
  },
  swiperBindchange(e) {
    var _this = this
    console.log(e)
    _this.initAbout(_this.data.vipForm[e.detail.current].name)
    _this.data.vipMethod = _this.data.vipForm[e.detail.current].method
    if (_this.data.vipForm[e.detail.current].hasOwnProperty('title')) {
      _this.data.tagId = _this.data.vipForm[e.detail.current].tagid
      _this.data.bottonTitle = _this.data.vipForm[e.detail.current].title
    } else {
      _this.data.tagId = null
      _this.data.bottonTitle = '立即开通'
    }
    console.log(_this.data.vipForm[e.detail.current].tagid)
    this.setData({
      currentSwiperIndex: e.detail.current,
      bottonTitle: _this.data.bottonTitle,
      tagId: _this.data.tagId,
      vipMethod:_this.data.vipMethod
    })
  },
  initAbout(name) {
    var _this = this
    _this.data.content = ''
    _this.data.htmlcontent = ''
    var name = name
    let params = {
      'mchId': app.globalData.mchId,
      'type': 0,
    }
    tag(params).then(res => {
      console.log(res)
      let htmled = false;
      var tagForm = []
      res.data.data.forEach(item => {
        if(item.name === name){
          tagForm.push(item)
        }
      })
      if (tagForm.length > 0) {
        this.setData({
          tagIds: tagForm[0].id,
          version: tagForm[0].memo,
          img: tagForm[0].icon
        })
        tagList({ 'mchId': app.globalData.mchId, 'tagIds': this.data.tagIds }).then(res => {
          console.log(res)
          if (res.data.data.length > 0) {
            console.log('33333')
            _this.data.contentType = true
            this.setData({
              contentType: _this.data.contentType,
              htmlcontent: res.data.data[0].content,
            })
          } else {
            console.log('222222')
            this.data.content = _this.data.vipForm[_this.data.currentSwiperIndex].memo,
              _this.data.contentType = false
            _this.setData({
              content: _this.data.content,
              contentType: _this.data.contentType
            })
          }
          console.log(_this.data.contentType)
          console.log('debug——html')
        }).catch(err => {
          console.log(err)
        })
      } else {
        console.log('11111')
        this.data.content = _this.data.vipForm[_this.data.currentSwiperIndex].memo,
        _this.data.contentType = false
        _this.setData({
          content: _this.data.content,
          contentType: _this.data.contentType
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
  swiperClick(e) {
    var _this = this
    console.log(e)
    _this.data.currentSwiperIndex = e.currentTarget.dataset.index
    _this.setData({
      currentSwiperIndex: _this.data.currentSwiperIndex
    })
  },
  getInfo() {
    var _this = this
    getInfo().then(
      res => {
        console.log(res)
        if (res.type == 'success') {
          this.setData({
            vip: util.isNull(res.data.member.vipName) ? "vip" + res.data.member.vip : res.data.member.vipName,
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },
  vipBotton() {
    var _this = this;
    if(_this.data.vipMethod == 3){
      wx.navigateTo({
        url: '/pages/member/vip/vip',
      })
    }else{
      wxPageList({
        mchId: app.globalData.mchId,
        tagIds: _this.data.tagId,
      }).then(res => {
        if (res.data.data.length > 0) {
          wx.navigateTo({
            url: '/pages/index/wxPage/wxPage?id=' + res.data.data[0].id
          })
        }
      })
    }
  }

})
